package com.huobao.yunying.activitys.muyingquan;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.simple.eventbus.EventBus;
import org.simple.eventbus.Subscriber;
import org.xutils.common.util.LogUtil;
import org.xutils.view.annotation.ViewInject;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.huobao.yunying.adapters.Adapter_All_Tiezi;
import com.huobao.yunying.adapters.Adapter_Quanzi_Zhiding;
import com.huobao.yunying.base.HuobaoBaseActivity;
import com.huobao.yunying.https.Http;
import com.huobao.yunying.https.HttpListener;
import com.huobao.yunying.https.HttpUtil;
import com.huobao.yunying.utils.ImageUtils;
import com.huobao.yunying.views.NoScrollListView;
import com.huobao.zhangying.R;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;

public abstract class Act_QuanziDetail_Base extends HuobaoBaseActivity
		implements OnScrollListener, OnItemClickListener {

//	@ViewInject(R.id.scrollview)
//	private ScrollView scrollview;

	// 头部圈子信息
	// @ViewInject(R.id.imgView)
	protected ImageView imgView;
	// @ViewInject(R.id.tv_name)
	protected TextView tv_name;
	// @ViewInject(R.id.tv_content)
	protected TextView tv_content;
	// @ViewInject(R.id.tv_number)
	protected TextView tv_number;
	// 头部结束---------------------------------------
	// 置顶ListView
	// @ViewInject(R.id.lv_zhiding)
	protected NoScrollListView lv_zhiding;
	@ViewInject(R.id.lv_all)
	protected ListView lv_all;
	// 置顶适配器
	protected Adapter_Quanzi_Zhiding adapter_zhiding;
	// 全部帖子适配器
	protected Adapter_All_Tiezi adapter_all;
	// 圈子id
	protected String id;

	protected int m_CurrentPage = 1;
	protected TextView tv_FootMsg;
	protected boolean isLoading = true;

	@Override
	protected int getContentView() {
		return R.layout.layout_quanzidetai_all;
	}

	private void initHeadView(View headView) {
		imgView = (ImageView) headView.findViewById(R.id.imgView);
		tv_name = (TextView) headView.findViewById(R.id.tv_name);
		tv_content = (TextView) headView.findViewById(R.id.tv_content);
		tv_number = (TextView) headView.findViewById(R.id.tv_number);
		lv_zhiding = (NoScrollListView) headView.findViewById(R.id.lv_zhiding);
	}

	@Override
	protected String getTitleText() {
		return null;
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		EventBus.getDefault().unregister(this);
	}

	@Override
	protected void init() {
		EventBus.getDefault().register(this);
		View HeadView = LayoutInflater.from(context).inflate(R.layout.item_quanzi_detail_header, null);
		initHeadView(HeadView);
		lv_all.addHeaderView(HeadView);
		mAbTitleBar.setVisibility(View.GONE);
		id = getIntent().getStringExtra("id");
		adapter_zhiding = new Adapter_Quanzi_Zhiding(context);
		lv_zhiding.setAdapter(adapter_zhiding);

		lv_zhiding.setOnItemClickListener(this);
		lv_all.setOnItemClickListener(this);

		View footView = LayoutInflater.from(context).inflate(R.layout.item_footview, null);
		tv_FootMsg = (TextView) footView.findViewById(R.id.tv_message);
		lv_all.addFooterView(footView,null,false);
		adapter_all = new Adapter_All_Tiezi(context);
		lv_all.setAdapter(adapter_all);
		lv_all.setOnScrollListener(this);
		getStoreMsgAndZhidingList();
		onInit();
	}

	private void getStoreMsgAndZhidingList() {
		Map<String, String> map = HttpUtil.getParams("app", "postbar", "id", id);
		Http.getInstant().sendRequest(context, map, new HttpListener() {

			@Override
			public void onHttpSuccess(String result) {
				JSONObject jsonObject = JSON.parseObject(result);
				Quanzi quanzi = JSON.parseObject(jsonObject.getString("retval"), Quanzi.class);
				List<FristHostTiezi> list = JSON.parseArray(jsonObject.getJSONObject("retval").getString("posts_list"),
						FristHostTiezi.class);
				ImageUtils.loadimg(imgView, quanzi.getAvatar());
				tv_name.setText(quanzi.getGname());
				tv_content.setText(quanzi.getIntro());
				tv_number.setText("帖子：" + quanzi.getPosts());
				adapter_zhiding.addAll(list);
//				scrollview.smoothScrollTo(0, 0);
			}

			@Override
			public void onHttpError(String error) {
				// TODO Auto-generated method stub

			}
		});
	}

	@Subscriber(tag = "scroll")
	public void scroll(String s) {
//		scrollview.smoothScrollTo(0, 0);
	}

	protected void getAllTiezi(boolean showDialog, boolean isJinghua) {
		Map<String, String> map = HttpUtil.getParams("app", "postbar", "act", isJinghua ? "betterpost" : "grouppost",
				"id", id, "page", m_CurrentPage + "");
		Http.getInstant().sendRequest(context, showDialog, map, new HttpListener() {

			@Override
			public void onHttpSuccess(String result) {
				JSONObject jsonObject = JSON.parseObject(result);
				JSONArray array = jsonObject.getJSONArray("retval");
				List<FristHostTiezi> list = new ArrayList<FristHostTiezi>();
				for (int i = 0; i < array.size(); i++) {
					JSONObject object = array.getJSONObject(i);
					FristHostTiezi tiezi = JSON.parseObject(object.toString(), FristHostTiezi.class);
					List<String> images = JSON.parseArray(object.getString("images"), String.class);
					tiezi.setImages(images);
					list.add(tiezi);
				}
				if (list == null || list.size() == 0) {
					tv_FootMsg.setText("加载完成");
					isLoading = true;
					return;
				}
				adapter_all.addAll(list);
				isLoading = false;
//				scrollview.smoothScrollTo(0, 0);
			}

			@Override
			public void onHttpError(String error) {
				// TODO Auto-generated method stub

			}
		});
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
		if ((firstVisibleItem + visibleItemCount) >= totalItemCount) {
			if (isLoading) {
				return;
			}
			isLoading = true;
			m_CurrentPage++;
			// getAllTiezi(false);
			onScroll();
		}
	}

	protected abstract void onScroll();

	protected abstract void onInit();

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long lid) {
		FristHostTiezi tiezi = null;
		if (parent.equals(lv_all)) {
			LogUtil.e("position="+position);
			if(position == 0)return;
			tiezi = adapter_all.getItem(position-1);
		} else if (parent.equals(lv_zhiding)) {
			tiezi = adapter_zhiding.getItem(position);
		}
		if (tiezi == null)
			return;
		Intent intent = new Intent(this, Act_Tiezi_Detail.class);
		intent.putExtra("quanziid", id);
		intent.putExtra("id", tiezi.getPid());
		startActivity(intent);
	}
}
